<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\NoticeModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use think\Db;
use think\Image;


class AdminNoticeController extends AdminBaseController
{
    //列表
    public function index(){
        
        $log_list = Db::name('notice')
            ->alias('a')
            ->join('cmf_notice_sort b','a.sort_id=b.id')
            ->field('a.*,b.name')
            ->where(['a.delete_time'=>0])
            ->order("a.time DESC")
            ->paginate(10);
        // 获取分页显示
        $page = $log_list->render();

       
        $this->assign("page", $page);
  
        $this->assign("data", $log_list);
        return $this->fetch();
    }
    //添加页面
    public function add(){
        $sort = Db::name('notice_sort')
            ->column('id,name');
        $this->assign('sort',$sort);
        return $this->fetch();
    }
    //添加提交
    public function addPost()
    {   
        
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];
            
            if (empty($post['title'])) {
                $this->error("标题不能为空！");
            }
            if (empty($post['sort_id'])) {
                $this->error("分类不能为空");
            }
            if (empty($post['content'])) {
                $this->error("内容不能为空");
            }
            if (empty($post['intro'])) {
                $this->error("简介不能为空");
            }
            if (empty($post['user_id'])) {
                $this->error("发布者不能为空");
            }
            $post['time'] = strtotime($post['time']);
            $post['posttime'] = time();

            $res = Db::name('notice')->insertGetId($post);
            if ($res) {
                //操作日志
                admin_log($post['title'], 'add', '公告动态文章');
                Db::name('new_msg')->insert(
                    [
                        'table_name'=>'notice',
                        'time'=>time(),
                        'department_id'=>0,
                        'content'=> $post['title'],
                        'object_id'=>$res,
                        'type'=>$post['sort_id']
                    ]);
                $this->success('添加成功!', url('AdminNotice/index'));
            }else{
                $this->error('添加失败');
            }
            
        }

    }
    //编辑
    public function edit(){
        $sort = Db::name('notice_sort')
            ->column('id,name');
        $this->assign('sort',$sort);
        $id = $this->request->param('id', 0, 'intval');
        $data = Db::name('Notice')->find($id);
        $this->assign('result',$data);
        return $this->fetch();
    }
    //编辑提交
    public function editPost(){
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];
            
            if (empty($post['title'])) {
                $this->error("标题不能为空！");
            }
            if (empty($post['sort_id'])) {
                $this->error("分类不能为空");
            }
            if (empty($post['content'])) {
                $this->error("内容不能为空");
            }
            if (empty($post['intro'])) {
                $this->error("简介不能为空");
            }
            if (empty($post['user_id'])) {
                $this->error("发布者不能为空");
            }
            $post['time'] = strtotime($post['time']);
            $post['posttime'] = time();
            $res = Db::name('Notice')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                //操作日志
                admin_log($post['title'], 'edit', '公告动态文章');

                $this->success('编辑成功!', url('AdminNotice/index'));
            }else{
                $this->error('编辑失败');
            }
            
        }
    }
    //删除提交
    public function delete(){
        $param   = $this->request->param();
        if (isset($param['id'])) {
            $id   = $this->request->param('id', 0, 'intval');
            $data = Db::name('Notice')->find($id);
            $res = Db::name('Notice')->where(['id'=>$id])->update(['delete_time'=>time()]);
            if ($res) {
                $data   = [
                    'object_id'   => $data['id'],
                    'create_time' => time(),
                    'table_name'  => 'notice',
                    'name'        => $data['title'],
                    'site_id'     => 1,  //站点ID
                ];
                Db::name('recycleBin')->insert($data);
                //操作日志
                admin_log($data['name'], 'delete', '公告动态文章');

                $this->success('删除成功!', url('AdminNotice/index'));
            }else{
                $this->error('删除失败');
            }
        }
    }
}
